﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace TLSharp
{
    public class GetTurnStateResponse
    {
        public string MessageType = "";
        public int OwnedTeamID = -1;
        public int TurnID = -1;
        public int NextTurnTeamID = -100;
        public DateTime DateAdded;
        public DateTime DateExpires ;
        public string GameState = "";
        public int GameID = -1;
        public bool bStarted = false;
        public bool bComplete = false;
        public bool HasAd = false;
        public string AdType = "";
        public string AdURL = "";
        public string AdTitle = "";
        public string AdText = "";
        public string AdWebTag = "";
        public string AdFooter = "";
        public int GameType = -1;
        public DateTime  GameDate ;
        public DateTime  GameTime ;
        public DateTime  DateComplete ;
        public List<string> GameTeams;
        public List<string> GameNotes;
        private List<GameTeam> _GameTeamObjects = null;
        private List<TeamNote> _GameNotesObjects = null;
        public int ErrorCode = -1;
        public string UserMessage;

        public GameTeam Challenger
        {
            get
            {
                foreach (GameTeam ThisTeam in GameTeamObjects)
                {
                    if (ThisTeam.GameTeamTypeID == 1) { return ThisTeam; }
                }
                return null;
            }
        }

        public GameTeam Challengee
        {
            get
            {
                foreach (GameTeam ThisTeam in GameTeamObjects)
                {
                    if (ThisTeam.GameTeamTypeID == 2) { return ThisTeam; }
                }
                return null;
            }
        }

        public List<TeamNote> GameNotesObjects
        {
            get
            {
                if (_GameNotesObjects == null && GameNotes != null )
                {
                    _GameNotesObjects = new List<TeamNote>();
                    foreach (string ThisString in GameNotes)
                    {
                        Newtonsoft.Json.Linq.JObject o = JObject.Parse(ThisString);

                        TeamNote b = (TeamNote)(new JsonSerializer()).Deserialize(new JsonTokenReader(o), typeof(TeamNote));

                        JValue ThisValue = new JValue(ThisString);
                        _GameNotesObjects.Add(b);
                    }
                }
                return _GameNotesObjects;
            }
        }

        public List<TeamNote> PrivateNotes
        {
            get
            {
                return GameNotesObjects.Where(p => p.bPrivate && p.PrivateNoteOwners.Contains(OwnedTeamID)).OrderByDescending(p => p.MostRecentAdded).ToList();
            }
        }

        public List<TeamNote> Comments
        {
            get
            {
                return GameNotesObjects.Where(p => !PrivateNotes.Contains(p)).OrderByDescending(p => p.MostRecentAdded).ToList();
            }
        }

        public List<TeamNote> FlatComments
        {
            get
            {
                return Comments.SelectMany(p => p.Flat).OrderByDescending(p => p.MostRecentAdded).ToList();
            }
        }

        public TeamNote FindRoot(TeamNote ThisNote)
        {
            if (ThisNote.ReplyToNoteID == -1)
            { return ThisNote; }
            else
            {
                foreach (TeamNote ANote in FlatComments)
                {
                    if (ANote.NoteID == ThisNote.ReplyToNoteID)
                    {
                        return FindRoot(ANote);
                    }
                }
            }
            return null;

        }

        public List<TLSharp.GameTeam> GameTeamObjects
        {
            get
            {
                if (_GameTeamObjects == null && (GameTeams!=null && GameTeams.Count==2))
                {
                    _GameTeamObjects = new List<GameTeam>();
                    foreach (string ThisString in GameTeams)
                    {
                        Newtonsoft.Json.Linq.JObject o = JObject.Parse(ThisString);

                        GameTeam b = (GameTeam)(new JsonSerializer()).Deserialize(new JsonTokenReader(o), typeof(GameTeam));

                        JValue ThisValue = new JValue(ThisString);
                        _GameTeamObjects.Add(b);
                    }
                }
                return _GameTeamObjects;
            }
        }
    }
}
